'use strict';

import React, { Component } from 'react';
import * as MColor from '../../config/meterialColor';
import Mymodal from '../usefulcomponent/mymodal';//引入后一定要用否则报错
import ServerConfig from '../../config/serverConfig';
import Base64Binary from '../../../function/base64-binary';
import {
  	StyleSheet,
  	View,
  	Text,
  	TouchableNativeFeedback,
    NativeModules,
} from 'react-native';

class applist_listitem extends Component {

	constructor(props) {
	  super(props);
		
	  this.state = {
	  	modalvisible:false,
	  };
	}
	_onPressButton=()=>{
		
	};
	//弹出modal后按下了取消按钮
	_onPressModalClose=()=>{
		this.setState({
			modalvisible:false
		})
	};
	//弹出modal
	_openModal=()=>{
		this.setState({
			modalvisible:true
		})
	};
	//弹出modal后按下确定按钮
	_onPressModalok(){
		//http://127.0.0.1:7777/appdown/SYS.base54
		fetch(ServerConfig.APP_DOWN+this.props.appname)
	      .then(res=>res.text())
	      .then((data)=>{
	      	var bin;
	        bin=Base64Binary.decode(data);
	       	//发送二进制的程序代码
	        this._sendappbybluetooth(bin);
	        console.log(bin);
	        //关闭modal
	        this.setState({
	        	modalvisible:false
	        });
	      });
	};
	//通过蓝牙传输要下载的app
	_sendappbybluetooth(appbin){	   	
	   	console.log(appbin);
	   	//对于一个数组最后一个元素为记录他的长度，就是length属性，这个数是发不到java的，如果你发过去是报错的
	   	//java的那个函数无法解析一个大于255的数,所以appbin.length-1
	   	var bytes=appbin.slice(0,appbin.length-1);
	   	//var heads=[0xa8,0x01,0x12,0x8c];//4748
	   	var heads=[0xa8,0x01];//0xa8代表起始头，0x01代表功能码01升级程序
	   	//起始头位 0xa8+功能码0x01+程序体的长度
	   	heads.push( Math.floor((appbin.length-1)/256) );//添加一个高位作为要发送的数据长度
	   	heads.push((appbin.length-1)%256);//添加一个低位
	   	console.log(heads);
	    //NativeModules.BluetoothAndroid.socketwrite(heads);//发送数据头
	    //发送数据头,使用回调来传递结果
	    NativeModules.BluetoothAndroid.socketwriteReCallback(
	    	heads,
	    	2,//代表需要接收1个字节的数据
	    	(errmsg)=>{
	    		console.log(msg);//返回错误的回调
	    	},
	    	(returnbytes)=>{
	    		//console.log(returnbytes);
	    		//成功返回的是一个数组
	    		console.log(returnbytes);
	    		//如果单片机回复0xa8 0x80，那么就代表那边已经擦除好了50k内存，死循环等待你发送程序代码
	    		if(returnbytes[0]==(-88) && returnbytes[1]==(-128)){
	    			console.log(bytes);
	    			NativeModules.BluetoothAndroid.socketwrite(bytes);//发送程序体
	    		}
	    	}
	    );
	   	//NativeModules.BluetoothAndroid.socketwrite(bytes);//发送程序体
	};
	render() {
	    return (
	    	<View style={styles.contain}>
	    		<Mymodal
	    			title={'是否使用'+this.props.appname+'?'}
	    			visible={this.state.modalvisible}
	    			close={this._onPressModalClose}
	    			ok={this._onPressModalok.bind(this)}
	    		></Mymodal>
				<View style={styles.view2}>
      				<Text style={styles.title}>{this.props.appname}</Text>
				</View>
				<TouchableNativeFeedback onPress={this._openModal}>
					<View style={styles.buttonview}>
	        			<Text style={{fontSize:25,padding:3,color:'white'}}>升级</Text>
	      			</View>
				</TouchableNativeFeedback>

	    	</View>
	    );
	}
}

const styles = StyleSheet.create({
	contain:{
		flex:1,
		flexDirection:'row',
		alignItems:'center',
		height:60,
		padding:2,
		borderColor:'#dddddd',
    	borderWidth:1,
    	marginBottom:9,
    	backgroundColor:MColor.LIGHT_PRIMARY_COLOR,
	},
	view2:{
		flex:5,
	},
	buttonview:{
		flex:2,
		justifyContent:'center',
		alignItems:'center',
		backgroundColor:MColor.ACCENT_COLOR,
		elevation:10,

	},
	title:{
		fontSize:20,
		color:MColor.PRIMARY_TEXT
	},

});


export default applist_listitem;